package com.globefrog.framework.log.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
import java.nio.charset.Charset;
import lombok.extern.slf4j.Slf4j;

/**
 * IO工具类.
 *
 * @author caijinbang
 * @date 2020-11-05 22:07
 */
@Slf4j
public class IOUtils {

  private static final int BUFFER_SIZE = 4096;

  private IOUtils() {
  }

  public static void write(File file, byte[] bytes) {
    BufferedOutputStream stream = null;
    try {
      stream = new BufferedOutputStream(new FileOutputStream(file));
      stream.write(bytes);
    } catch (IOException var7) {
      throw new RuntimeException(var7);
    } finally {
      close(stream);
    }

  }

  public static void write(File file, String content) {
    write(file, content.getBytes(Charset.defaultCharset()));
  }

  public static String text(Reader reader) {
    Reader bufferedReader = reader;
    if (!(reader instanceof BufferedReader)) {
      bufferedReader = new BufferedReader(reader);
    }

    StringBuilder builder = new StringBuilder();
    char[] buffer = new char[BUFFER_SIZE];
    try {
      while (true) {
        int length = (bufferedReader).read(buffer);
        if (length == -1) {
          String var5 = builder.toString();
          return var5;
        }

        builder.append(buffer, 0, length);
      }
    } catch (IOException var9) {
      throw new RuntimeException(var9);
    } finally {
      close(reader);
    }
  }

  public static String text(File file) {
    BufferedInputStream stream = null;

    String e;
    try {
      stream = new BufferedInputStream(new FileInputStream(file));
      e = new String(bytes(stream), Charset.defaultCharset());
    } catch (IOException var6) {
      throw new RuntimeException(var6);
    } finally {
      close(stream);
    }

    return e;
  }

  public static String text(InputStream stream) {
    return new String(bytes(stream), Charset.defaultCharset());
  }

  public static byte[] bytes(File file) {
    BufferedInputStream stream = null;

    byte[] e;
    try {
      stream = new BufferedInputStream(new FileInputStream(file));
      e = bytes(stream);
    } catch (FileNotFoundException var6) {
      throw new RuntimeException(var6);
    } finally {
      close(stream);
    }

    return e;
  }

  public static byte[] bytes(InputStream stream) {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    byte[] buf = new byte[4096];

    try {
      while (true) {
        int len = stream.read(buf);
        if (len < 0) {
          return byteArrayOutputStream.toByteArray();
        }

        byteArrayOutputStream.write(buf, 0, len);
      }
    } catch (IOException var5) {
      throw new RuntimeException(var5);
    }
  }

  public static void close(InputStream stream) {
    try {
      if (stream != null) {
        stream.close();
      }
    } catch (IOException e) {
      log.info(e.getMessage(), e);
    }

  }

  public static void close(OutputStream stream) {
    try {
      if (stream != null) {
        stream.flush();
        stream.close();
      }
    } catch (IOException e) {
      log.info(e.getMessage(), e);
    }

  }

  public static void close(Reader reader) {
    try {
      if (reader != null) {
        reader.close();
      }
    } catch (IOException e) {
      log.info(e.getMessage(), e);
    }

  }

  public static void close(Writer writer) {
    try {
      if (writer != null) {
        writer.flush();
        writer.close();
      }
    } catch (IOException e) {
      log.info(e.getMessage(), e);
    }

  }

  public static void flush(OutputStream stream) {
    try {
      if (stream != null) {
        stream.flush();
      }
    } catch (IOException e) {
      log.info(e.getMessage(), e);
    }

  }

  public static void flush(Writer writer) {
    try {
      if (writer != null) {
        writer.flush();
      }
    } catch (IOException e) {
      log.info(e.getMessage(), e);
    }
  }

  public static String getTempFolderPath() {
    return System.getProperty("java.io.tmpdir");
  }

  public static void copy(File from, File to) {
    InputStream input = null;
    BufferedOutputStream output = null;
    try {
      input = new BufferedInputStream(new FileInputStream(from));
      output = new BufferedOutputStream(new FileOutputStream(to));
      byte[] buf = new byte[BUFFER_SIZE];
      while (true) {
        int len = input.read(buf);
        if (len < 0) {
          return;
        }
        output.write(buf, 0, len);
      }
    } catch (IOException e) {
      throw new RuntimeException(e);
    } finally {
      close(input);
      close(output);
    }
  }
}

